import { type } from 'os'
import { DB } from '~/server/db.ts'
DB.note.belongsTo(DB.user)
interface ValueObject {
  rows: [
    {
      title: string
    }
  ]
  meta: any
}
export default defineEventHandler(async (event) => {
  const query = getQuery(event)
  let {
    page = 1, size = 10
  } = query
  page = Number(page)
  size = Number(size)
  const {
    count,
    rows
  } = await DB.note.findAndCountAll({
    offset: (page - 1) * size,
    limit: size,
    order: [
      ['createdAt', 'DESC']
    ],
    where: {
      status: 1, // 显示状态为可见
      check: 1
    },
    include: [{
      model: DB.user,
      attributes: ['id', 'name', 'avatar']
    }]
  })
  const data: ValueObject = {
    rows,
    meta: {
      size, // 每页条目
      count: count, // 条目总数
      page: page, // 当前页数
      total: Math.ceil(count / size), // 总页数
    }
  }
  return data
})
